# !/usr/local/bin/python3
# -*- encoding: utf-8 -*-
# @FileName :   MailSender.py
# @DateTime :   2022/8/25 | 21:08
# @Author   :   IoTus3
# @GitHub   :   https://gitee.com/IoTus3
# @Contact  :   xianglin.fu3@gmail.com
# @License  :   (C)Copyright 2022-2023, LinYuStudio-NLPR-CASIA
# @Editor   :   Power by PyCharm
# @Desc     :   邮件发送类-暂不实现


# 导入smtp相关库
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL



class MailSender(object):

    def __init__(self, host, sender, pwd, receiver, title, price):
        # smtp服务器
        self.host = host
        # 发送者邮箱
        self.sender = sender
        # pwd为126邮箱授权码
        self.pwd = pwd
        # 收件人邮箱
        self.receiver = receiver
        # 商品
        self.title = title
        # 价格
        self.price = price

    def send(self):
        try:
            # 邮件正文内容
            content = self.title + "的价格降到了80%的点了，现在价格为" + str(self.price)
            header = self.title + "降价了"
            # ssl登录
            smtp = SMTP_SSL(self.host)
            # set_debuglevel()是用来调试的。参数值为1表示开启调试模式，参数值为0关闭调试模式
            smtp.set_debuglevel(1)
            smtp.ehlo(self.host)
            smtp.login(self.sender, self.pwd)

            msg = MIMEText(content, "plain", "utf-8")
            msg["Subject"] = Header(header, "utf-8")
            msg["From"] = self.sender
            msg["To"] = self.receiver
            smtp.sendmail(self.sender, self.receiver, msg.as_string())
            smtp.quit()
            return True
        except Exception as e:
            # error = str(e)
            return False

